package ext.acae2.document.dataUtility;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import wt.httpgw.URLFactory;
import wt.util.WTException;

import com.ptc.core.components.descriptor.ModelContext;
import com.ptc.core.components.factory.AbstractDataUtility;
import com.ptc.core.components.rendering.guicomponents.GUIComponentArray;
import com.ptc.core.components.rendering.guicomponents.TextDisplayComponent;
import com.ptc.core.components.util.OidHelper;
import com.ptc.netmarkets.util.beans.NmCommandBean;

import ext.acae2.document.mvc.ECMReplyDocBean;
import ext.acae2.document.mvc.builders.ECMRecommendTableBuilder;

/**
 * @desription TODO
 * @author cnie
 * @date 2013-7-16 上午9:53:28
 */
public class ECMReplyDetailDataUtility extends AbstractDataUtility{

	/* (non-Javadoc)
	 * @see com.ptc.core.components.descriptor.DataUtility#getDataValue(java.lang.String, java.lang.Object, com.ptc.core.components.descriptor.ModelContext)
	 */
	@Override
	public Object getDataValue(String column, Object obj, ModelContext context)
			throws WTException {
		
		NmCommandBean commandBean = context.getNmCommandBean();
		List<ECMReplyDocBean> ecmReplyBeans = (List<ECMReplyDocBean>) commandBean.getRequest().getSession().getAttribute("initECMReplyDocBeans");
		GUIComponentArray guicomponentarrayMain = new GUIComponentArray();
		TextDisplayComponent tdc = new TextDisplayComponent("");
		String currentObjOid = OidHelper.getNmOid(obj).toString();
		URLFactory uf = new URLFactory();
		if(ecmReplyBeans!=null){
			for(ECMReplyDocBean epmDocBean:ecmReplyBeans){
				if(currentObjOid.indexOf(epmDocBean.getECMDocOid())>-1){
					if(ECMRecommendTableBuilder.ECM_REPLY_STATE.equals(column))
						tdc.setValue(epmDocBean.getECMReplyState());
					else if(ECMRecommendTableBuilder.REPLY_ECM_NUMBER.equals(column)){
						if(StringUtils.isNotBlank(epmDocBean.getECMReplyDocOid())){
							HashMap<String, String> params = new HashMap<String, String>();
							params.put("oid", epmDocBean.getECMReplyDocOid());
							String infoUrl = uf.getHREF("app/#ptc1/tcomp/infoPage", params, true);
							StringBuffer linkInfoSb = new StringBuffer();
							linkInfoSb
									.append("<a href=\" javascript:void(0);\"  onclick=");
							linkInfoSb
									.append("\"window.open('"
											+ infoUrl
											+ "','');return false\">");
							linkInfoSb.append(epmDocBean.getECMReplyNumber() + "</a>");
							tdc.setCheckXSS(false);
							tdc.setValue(linkInfoSb.toString());
						}else{
							tdc.setValue(epmDocBean.getECMReplyNumber());
						}
					}
					break;
				}
			}
		}
		guicomponentarrayMain.addGUIComponent(tdc);
		return guicomponentarrayMain;
	}
	

}
